layui.config({
    base: '/static/js/lib',
    version: new Date().getTime()
}).use(['layer', "table"], function () {

    $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    form.on('submit(sreach)', function (data) {
        tableRender(data.field);
        return false;
    });

    function tableRender(queryCondition) {
        var url = "/course/page";
        for (var key in queryCondition) {
            var condition = queryCondition[key];
            if (null != condition && condition != "" && key != "tenant") {
                url += ("?" + key + "=" + condition);
            }
        }
        table.render({
            elem: '#wait-page',
            url: url,
            page: true,
            cols: [[ //表头
                {field: 'courseId', title: 'ID', width: "10%", fixed: 'left'}
                , {field: 'courseName', title: '课程名称', width: "80%"}
                , {field: 'add', title: '操作', width: "10%", templet: "#add"}
            ]]
        });
    }

    tableRender({});

    table.on('tool(wait-page)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent == "course-add") {
            thisUtil.add(data.courseId, data.courseName);
        }
    });

    table.on('tool(course-page)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent == "del") {
            thisUtil.del(data.id)
        }
    });

    $(".isOk").click(function () {
        if (thisUtil.selectJson.length <= 0) {
            layer.msg("请至少选择1门以上的课程");
        } else {
            parent.$("textarea[name='coursejson']").val(JSON.stringify(thisUtil.selectJson));
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        }
    });
    var thisUtil = {
        selectJson: [],
        add: function (id, name) {
            var course = {id: id, name: name};
            for (i = 0; i < thisUtil.selectJson.length; i++) {
                if (id == thisUtil.selectJson[i].id) {
                    layer.msg("已经添加！请勿重复添加");
                    return;
                }
            }
            thisUtil.selectJson.push(course);
            thisUtil.refresh();

        },
        del: function (id) {
            for (i = 0; i < thisUtil.selectJson.length; i++) {
                if (id == thisUtil.selectJson[i].id) {
                    thisUtil.selectJson.splice(i, 1);
                }

            }
            thisUtil.refresh();
        },
        refresh: function () {
            table.render({
                elem: '#course-page',
                data: thisUtil.selectJson,
                limit: thisUtil.selectJson.length,
                cols: [[ //表头
                    {field: 'id', title: 'ID', width: "10%", fixed: 'left'}
                    , {field: 'name', title: '课程名称', width: "80%"}
                    , {field: 'remove   ', title: '操作', width: "10%", templet: "#remove"}
                ]]
            });
        },
        aa: function (coursejson) {
            if (null == coursejson|| "" == coursejson){
                return false;
            }
            thisUtil.selectJson = JSON.parse(coursejson);
            table.render({
                elem: '#course-page',
                data: thisUtil.selectJson,
                limit: thisUtil.selectJson.length,
                cols: [[ //表头
                    {field: 'id', title: 'ID', width: "10%", fixed: 'left'}
                    , {field: 'name', title: '课程名称', width: "80%"}
                    , {field: 'remove   ', title: '操作', width: "10%", templet: "#remove"}
                ]]
            });
        }
    };
    thisUtil.aa(parent.$("textarea[name='coursejson']").val());

    $("#cateName").focus(function () {
        layer.open({
            type: 2,
            area: ['400px', '500px'],
            maxmin: true,
            moveOut: false,
            shade: 0.4,
            async: true,
            title: '选择课程分类',
            content: "/courseCate/tree"
        });
    });

    window.cateTreeCallBack = function (node) {
        if (node.isSubCate) {
            $("#cateName").val(node.name);
            $("input[name='cateId']").val(node.id);
            $("input[name='cateIdPath']").val(node.catePath);
            return {isClose: true};
        }
        return {
            isClose: false,
            callback: function () {
                layer.msg("只能选择子分类");
            }
        };
    };


});